package alo.p4.work;
//删除所有相邻元素

import org.junit.Test;

public class RemoveAdjacentDuplicates {
    @Test
    public void test(){
        int[] A={1,9,6,8,8,8,0,1,1,0,6,5};
        int index = removeAdjacentDuplicates(A);
        for (int i = 0; i <=index; i++) {
            System.out.println(" "+A[i]);//1,9,5
        }
    }
    public int removeAdjacentDuplicates(int[] A){
        int stkptr=-1;
        int i=0;
        while (i< A.length){
            if (stkptr==-1||A[stkptr]!=A[i]){
                stkptr++;
                A[stkptr]=A[i];
                i++;
            }else {
                while (i< A.length&&A[stkptr]==A[i])
                    i++;
                stkptr--;
            }
        }
        return stkptr;
    }
}
